﻿using Caliburn.Micro;
using SD.Infrastructure.WPF.Caliburn.Aspects;
using SD.Infrastructure.WPF.Caliburn.Base;
using SD.Infrastructure.WPF.Extensions;
using SD.IOC.Core.Mediators;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ServiceModel.Extensions;
using System.Threading.Tasks;
using SlamDunk.MES.IAppService.DTOs.Inputs;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;

namespace SlamDunk.MES.Client.ViewModels.Execution
{
    /// <summary>
    /// 工单弹出层执行人员ViewModel
    /// </summary>
    public class ActualPersonsViewModel : ScreenBase
    {
        #region # 字段及构造器
        /// <summary>
        /// 作业管理服务契约接口代理
        /// </summary>
        private readonly ServiceProxy<IExecutionContract> _iExecutionContract;

        /// <summary>
        /// 窗口管理器
        /// </summary>
        private readonly IWindowManager _windowManager;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ActualPersonsViewModel(ServiceProxy<IExecutionContract> iExecutionContract, IWindowManager windowManager)
        {
            this._iExecutionContract = iExecutionContract;
            this._windowManager = windowManager;
        }

        #endregion

        #region 属性
        #region 工单人员要求列表
        /// <summary>
        /// 工单人员要求列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderPersonAskInfo> WorkOrderPersonAsks { get; set; }
        #endregion

        #region 工单实际人员列表
        /// <summary>
        /// 工单实际人员列表
        /// </summary>
        [DependencyProperty]
        public ObservableCollection<WorkOrderActualPersonInfo> WorkOrderActualPersons { get; set; }
        #endregion

        #region 工单Id
        /// <summary>
        /// 工单Id
        /// </summary>
        [DependencyProperty]
        public Guid WorkOrderId { get; set; }
        #endregion

        #region 工厂模型Id
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        [DependencyProperty]
        public Guid? FacilityId { get; set; }
        #endregion
        #endregion

        #region 方法

        #region 加载 —— async Task Load(WorkOrderInfo workOrderInfo)
        /// <summary>
        /// 加载
        /// </summary>
        public async Task Load(WorkOrderInfo workOrderInfo)
        {
            WorkOrderId = workOrderInfo.Id;
            FacilityId = workOrderInfo.FacilityId;
            WorkOrderPersonAsks = new ObservableCollection<WorkOrderPersonAskInfo>(await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrderPersonAsks(
               workOrderInfo.Id)));
            WorkOrderActualPersons = new ObservableCollection<WorkOrderActualPersonInfo>(await Task.Run(() => this._iExecutionContract.Channel.GetWorkOrderActualPersons(
               workOrderInfo.Id)));
        }
        #endregion

        #region 添加人员
        /// <summary>
        /// 添加人员
        /// </summary>
        public async void CreatePerson()
        {
            AddActualPersonsViewModel viewModel = ResolveMediator.Resolve<AddActualPersonsViewModel>();
            viewModel.Load(FacilityId);
            bool? result = await this._windowManager.ShowDialogAsync(viewModel);
            if (result == true)
            {
                CreateWorkOrderActualPerson(viewModel.SelectedPerson.Name, viewModel.SelectedPerson.Id, viewModel.PersonUse, viewModel.ActualStartTime, viewModel.ActualEndTime, viewModel.Sort, viewModel.Description);
            }
        }
        #endregion

        #region 删除工单实际人员
        /// <summary>
        /// 删除工单实际人员
        /// </summary>
        public async void RemoveWorkOrderActualPerson(WorkOrderActualPersonInfo workOrderActualPersonInfo)
        {
            this.Busy();
            //删除工单实际人员
            WorkOrderActualPersons.Remove(workOrderActualPersonInfo);
            this.Idle();
        }
        #endregion

        #region 创建实际人员明细 —— async Task CreateWorkOrderActualPerson()
        /// <summary>
        /// 创建实际人员明细
        /// </summary>
        /// <param name="personId">人员名称</param>
        /// <param name="personId">人员Id</param>
        /// <param name="personUse">人员用途</param>
        /// <param name="actualStartTime">实际开始时间</param>
        /// <param name="actualEndTime">实际结束时间</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public void CreateWorkOrderActualPerson(string name, Guid personId, string personUse, DateTime actualStartTime, DateTime actualEndTime, int sort, string description)
        {
            WorkOrderActualPersonInfo workOrderActualPersonInfo = new WorkOrderActualPersonInfo();
            workOrderActualPersonInfo.PersonId = personId;
            workOrderActualPersonInfo.PersonInfo = new PersonInfo() { Name = name };
            workOrderActualPersonInfo.PersonUse = personUse;
            workOrderActualPersonInfo.ActualStartTime = actualStartTime;
            workOrderActualPersonInfo.ActualEndTime = actualEndTime;
            workOrderActualPersonInfo.Sort = sort;
            workOrderActualPersonInfo.Description = description;
            WorkOrderActualPersons.Add(workOrderActualPersonInfo);
        }
        #endregion

        #region 提交 —— async void Submit()
        /// <summary>
        /// 提交
        /// </summary>
        public async void Submit()
        {
            #region # 验证

            #endregion

            List<WorkOrderActualPersonParam> workOrderActualPersonParamList = new List<WorkOrderActualPersonParam>();
            foreach (var workOrderActualPerson in WorkOrderActualPersons)
            {
                WorkOrderActualPersonParam workOrderActualPersonParam = new WorkOrderActualPersonParam();
                workOrderActualPersonParam.personId = workOrderActualPerson.PersonId;
                workOrderActualPersonParam.personUse = workOrderActualPerson.PersonUse;
                workOrderActualPersonParam.actualStartTime = workOrderActualPerson.ActualStartTime;
                workOrderActualPersonParam.actualEndTime = workOrderActualPerson.ActualEndTime;
                workOrderActualPersonParam.sort = workOrderActualPerson.Sort;
                workOrderActualPersonParam.description = workOrderActualPerson.Description;
                workOrderActualPersonParamList.Add(workOrderActualPersonParam);
            }
            this.Busy();
            await Task.Run(() => this._iExecutionContract.Channel.SaveWorkOrderActualPersons(
                WorkOrderId, workOrderActualPersonParamList));
            await base.TryCloseAsync(true);
            this.Idle();
        }
        #endregion

        #endregion
    }
}
